<?
    /**
     * Smarty plugin
     * @package Smarty
     * @subpackage plugins
     */


    /**
     * Smarty {load_menu} function plugin
     *
     * Type:     function
     * Name:     load_submenu
     * Purpose:  Load the menu into a predefined variable
     *   @param array
     *   @param Smarty
     *
     * This smarty plugin can be talked to using the following
     * optional template parameters:
     *   
     *   * var (string) -- name of variable where to load the 
     *                     data tree to
     *                     >> default: submenu
     *
     *   * node (int)   -- folder to start loading menu from
     *                     >> default: current node
     *
     *   * pages (bool) -- whether to load separate pages from
     *                     the tree
     *                     >> default: false
     *
     * @return string
     */
    function smarty_function_load_submenu($params, &$smarty){
        global $db, $nodeData; // create a link to the database connection

        //pre-process parameters
        //variable name
        if(empty($params['var'])){
            $var = 'submenu';
        }else{
            $var = $params['var'];
        }

        
        //node to start from
        if(empty($params['node'])){
            $node = null;
        }else{
            $node = (int)$params['node'];
        }
        
        /*
        //get the pages?
        if(!empty($params['pages'])){
            if($params['pages'] == 'true'){
                $pages = true;
            }else{
                $pages = false;
            }
        }else{
            $pages = false;
        }
        */
        
        
        //get the node list
        $menu = $db->getAll("SELECT s.id AS id, s.level AS level, get_path(id) as path, om.title AS title, om.published_pages AS published_pages FROM structure AS s LEFT JOIN objectmap AS om ON s.id = om.nodeid WHERE s.parent_id = ".(int)$nodeData['nodeid']);
        
        $smarty->assign($var,$menu);
    }
?>